<?php

/**
 * This is the model class for table "lths_trade_general".
 *
 * The followings are the available columns in table 'lths_trade_general':
 * @property integer $id
 * @property string $trade_date
 * @property string $trade_staff
 * @property integer $trade_method
 * @property integer trade_user_type
 * @property integer $producttype_code
 * @property string $product_id
 * @property integer $trade_number
 * @property string $imei
 * @property double $serialnumber
 * @property double $all_fee
 * @property double $check_money
 * @property double $ready_money
 * @property double $pos_money
 * @property double $transfer_money
 * @property double $all_money
 * @property double $trade_dept
 * @property string $examine_staff
 * @property double $dept_minus
 * @property string $remark
 * @property string $update_staff
 * @property string $create_time
 * @property string $update_time
 *
 * The followings are the available model relations:
 * @property Product $product
 * @property Producttype $producttypeCode
 * @property BTrademethod $tradeMethod
 * @property User $tradeStaff
 */
class TradeGeneral extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return TradeGeneral the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'lths_trade_general';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('trade_date, trade_method, producttype_code,product_id, trade_number', 'required'),
			array('trade_method, producttype_code, trade_number', 'numerical', 'integerOnly'=>true),
			array('all_fee, check_money, ready_money, pos_money, transfer_money, all_money, trade_dept, dept_minus', 'numerical'),
			array('trade_staff, serialnumber', 'length', 'max'=>20),
			array('product_id', 'length', 'max'=>15),
			array('imei, examine_staff', 'length', 'max'=>50),
			array('remark', 'length', 'max'=>500),
			array('trade_user_type, create_time, update_time, update_staff', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, trade_date, trade_staff, trade_method, producttype_code, product_id, trade_number, imei, all_fee, check_money, ready_money, pos_money, transfer_money, all_money, trade_dept, examine_staff, dept_minus, remark, create_time, update_time', 'safe', 'on'=>'search'),
		);
	}
	
	public function onAfterSave($event)
	{
	    
	    $profitModel = new Profit;
	    $profitModel->attributes=array(
	    	'trade_date'       => $this->trade_date,
	    	'trade_staff'      => $this->trade_staff,
			'trade_method'     => $this->trade_method,
			'trade_user_type'  => 5,
			'producttype_code' => $this->producttype_code,
			'product_id'       => $this->product_id,
			'bill_fee'         => 0,
			'phone_fee'        => $this->all_fee,
			'check_profit'     =>$this->getCheckProfit(),
			//'real_profit'      =>,
			//'remark'           =>,
			//'treaty_id'        => $this->id,
			'general_id'       =>$this->id,
			'trade_type'       => 1,
	    );
	    //$profitModel->setCheckProfit();
	    $profitModel->save();
	    
	}
	
	private function getCheckProfit()
	{
		$checkProfit=0;
		
		if($this->trade_method==5)
		{
			switch($this->producttype_code)
			{
				case 24://手机:iphone|非iphone
				    $iphoneProduct = array('MD128CH/A','MD198CH/A','MD235CH/A','MD239CH/A','MD297CH/A','MD298CH/A','MD299CH/A','MD300CH/A','MD662CH/A','MD663CH/A');
					if (in_array($this->product_id, $iphoneProduct)) 
					{
						$checkProfit=($this->all_fee)*0.03;//iphone:销售价格*0.03
					}
					else
					{
						$checkProfit=($this->all_fee)*0.1;//非iPhone:合约预存款*0.1
					}
					break;
				case 4:
					$checkProfit=$this->all_fee;//配件:销售价格
					break;
				case 22:
					$checkProfit=($this->all_fee)*0.2;//上网卡套包:销售价格*0.20
					break;
				case 5:
					$checkProfit=($this->all_fee)*0.1;//无线数据产品:销售价格*0.10
					break;
				case 15:
					$checkProfit=($this->all_fee)*0.1;//上网卡硬件:销售额*10%
					break;
			}
		}
		return $checkProfit;
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'product' => array(self::BELONGS_TO, 'Product', 'product_id'),
			'producttypeCode' => array(self::BELONGS_TO, 'Producttype', 'producttype_code'),
			'tradeMethod' => array(self::BELONGS_TO, 'BTrademethod', 'trade_method'),
			'tradeStaff' => array(self::BELONGS_TO, 'User', 'trade_staff'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'trade_date' => '日期',
			'trade_staff' => '销售人',
			'trade_method' => '销售方式',
			'trade_user_type'=>'用户类型',
			'producttype_code' => '产品类型',
			'product_id' => '规格型号',
			'trade_number' => '数量',
			'imei' => '串码',
			'serialnumber' => '号卡',
			'all_fee' => '销售价格',
			'check_money' => 'Check Money',
			'ready_money' => '现金',
			'pos_money' => 'POS机刷卡',
			
			'transfer_money' => '转账金额',
			'all_money' => '合计',
			'trade_dept' => '营业欠款',
			'examine_staff' => '审批人',
			'dept_minus' => '欠款核减',
			'remark' => '备注',
			'create_time' => 'Create Time',
			'update_time' => 'Update Time',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('trade_date',$this->trade_date,true);
		$criteria->compare('trade_staff',$this->trade_staff,true);
		$criteria->compare('trade_method',$this->trade_method);
		$criteria->compare('producttype_code',$this->producttype_code);
		$criteria->compare('product_id',$this->product_id,true);
		$criteria->compare('trade_number',$this->trade_number);
		$criteria->compare('imei',$this->imei,true);
		$criteria->compare('serialnumber',$this->imei,true);		
		$criteria->compare('all_fee',$this->all_fee);
		$criteria->compare('check_money',$this->check_money);
		$criteria->compare('ready_money',$this->ready_money);
		$criteria->compare('pos_money',$this->pos_money);
		$criteria->compare('transfer_money',$this->transfer_money);
		$criteria->compare('all_money',$this->all_money);
		$criteria->compare('trade_dept',$this->trade_dept);
		$criteria->compare('examine_staff',$this->examine_staff,true);
		$criteria->compare('dept_minus',$this->dept_minus);
		$criteria->compare('remark',$this->remark,true);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_time',$this->update_time,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	protected function beforeDelete()
	{
		Yii::log('onBeforeDelete','info');
		if(parent::beforeDelete())
		{
			Profit::model()->deleteAll('general_id='.$this->id);
			return true;
		}
		else
			return false;
	}
	public function behaviors(){  
    return array(  
        'CTimestampBehavior' => array(  
            'class' => 'zii.behaviors.CTimestampBehavior',  
            'createAttribute' => 'create_time',  
            'updateAttribute' => 'update_time',  
        )  
      );  
    }  
}